﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Gollie.Administration.Service
{
	public class AdministrationTasksConfigSectionHandler : ConfigurationSection
	{
		[ConfigurationProperty("Tasks", IsDefaultCollection = false)]
		[ConfigurationCollection(typeof(TaskCollection),
			AddItemName = "add",
			ClearItemsName = "clear",
			RemoveItemName = "remove")]
		public TaskCollection Tasks
		{
			get
			{
				TaskCollection taskCollection =	(TaskCollection)base["Tasks"];
				return taskCollection;
			}
		}
	}

	public class TaskConfigElement : ConfigurationElement
	{
		[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
		public string Name
		{
			get
			{
				return (string)this["name"];
			}
			set
			{
				this["name"] = value;
			}
		}

		[ConfigurationProperty("task", IsRequired = true)]
		public string Task
		{
			get
			{
				return (string)this["task"];
			}
			set
			{
				this["task"] = value;
			}
		}

		[ConfigurationProperty("frequency", IsRequired = true)]
		public string Frequency
		{
			get
			{
				return ((string)this["frequency"]).ToLower();
			}
			set
			{
				this["frequency"] = value;
			}
		}

		[ConfigurationProperty("onmonth", IsRequired = false, DefaultValue = "1")]
		[IntegerValidator(MaxValue = 12, MinValue = 1)]
		public int OnMonth
		{
			get
			{
				return (int)this["onmonth"];
			}
			set
			{
				this["onmonth"] = value;
			}
		}

		[ConfigurationProperty("onday", IsRequired = false, DefaultValue="1")]
		[IntegerValidator(MaxValue=31, MinValue=1)]
		public int OnDay
		{
			get
			{
				return (int)this["onday"];
			}
			set
			{
				this["onday"] = value;
			}
		}

		[ConfigurationProperty("onweekday", IsRequired = false, DefaultValue = "0")]
		[IntegerValidator(MaxValue = 6, MinValue = 0)]
		public int OnWeekDay
		{
			get
			{
				return (int)this["onweekday"];
			}
			set
			{
				this["onday"] = value;
			}
		}

		[ConfigurationProperty("onhour", IsRequired = false, DefaultValue = "0")]
		[IntegerValidator(MaxValue = 23, MinValue = 0)]
		public int OnHour
		{
			get
			{
				return (int)this["onhour"];
			}
			set
			{
				this["onhour"] = value;
			}
		}

		[ConfigurationProperty("onminute", IsRequired = false, DefaultValue = "0")]
		[IntegerValidator(MaxValue = 59, MinValue = 0)]
		public int OnMinute
		{
			get
			{
				return (int)this["onminute"];
			}
			set
			{
				this["onminute"] = value;
			}
		}

		[ConfigurationProperty("shortdesc", IsRequired = true)]
		public string ShortDesc
		{
			get
			{
				return (string)this["shortdesc"];
			}
			set
			{
				this["shortdesc"] = value;
			}
		}
	}

	public class TaskCollection : ConfigurationElementCollection
	{
		public TaskCollection()
		{
			TaskConfigElement task = (TaskConfigElement)CreateNewElement();
			Add(task);
		}

		public override ConfigurationElementCollectionType CollectionType
		{
			get
			{
				return ConfigurationElementCollectionType.AddRemoveClearMap;
			}
		}

		protected override ConfigurationElement CreateNewElement()
		{
			return new TaskConfigElement();
		}

		protected override Object GetElementKey(ConfigurationElement element)
		{
			return ((TaskConfigElement)element).Name;
		}

		public TaskConfigElement this[int index]
		{
			get
			{
				return (TaskConfigElement)BaseGet(index);
			}
			set
			{
				if (BaseGet(index) != null)
				{
					BaseRemoveAt(index);
				}
				BaseAdd(index, value);
			}
		}

		new public TaskConfigElement this[string name]
		{
			get
			{
				return (TaskConfigElement)BaseGet(name);
			}
		}

		public int IndexOf(TaskConfigElement task)
		{
			return BaseIndexOf(task);
		}

		public void Add(TaskConfigElement task)
		{
			BaseAdd(task);
		}
		protected override void BaseAdd(ConfigurationElement element)
		{
			BaseAdd(element, false);
		}

		public void Remove(TaskConfigElement task)
		{
			if (BaseIndexOf(task) >= 0)
				BaseRemove(task.Task);
		}

		public void RemoveAt(int index)
		{
			BaseRemoveAt(index);
		}

		public void Remove(string name)
		{
			BaseRemove(name);
		}

		public void Clear()
		{
			BaseClear();
		}
	}

}
